app.controller('candController',
				[		'$scope',
						'$q',
						'$state',
						'$window',
						'$uibModal',
						'candCandidateService',
						'cmSlideCacheService',
						'pagingFilter',
						'filterFilter',
						'slideCallbackService',
						'toInterviewService',
						function($scope, $q, $state, $window,$uibModal,
								candCandidateService, cmSlideCacheService,
								pagingFilter, filterFilter,
								slideCallbackService,toInterviewService) {
							console.log(slideCallbackService);
							candCandidateService()
									.then(
											function(result) {
												// $scope.candidates = result;
												candCandidateService()
														.then(
																function(result) {
																	var requestResult = filterFilter(result);
																	pagingFilter
																			.listLoad(
																					requestResult,
																					$scope.itemsPer);
																	$scope.totalItems = pagingFilter
																			.getPgInfo().resArylength;
																	$scope.candidates = pagingFilter
																			.getCurrentPage($scope.currPage);
																})
											}, function() {

											});
							$scope.currPage = 1;
							$scope.totalItems;
							$scope.itemsPer = 15;
							$scope
									.$watch(
											"keyword",
											function(newValue, oldValue) {
												candCandidateService()
														.then(
																function(result) {
																	var requestResult = filterFilter(
																			result,
																			newValue);
																	pagingFilter
																			.listLoad(
																					requestResult,
																					$scope.itemsPer);
																	$scope.totalItems = pagingFilter
																			.getPgInfo().resArylength;
																	$scope.candidates = pagingFilter
																			.getCurrentPage($scope.currPage);
																})
											});
							$scope.$watch('currPage', function(newValue,
									oldValue) {
								$scope.candidates = pagingFilter
										.getCurrentPage(newValue);
							});

							$scope.doTask = function(cand, $event) {
								$state.go('recrut2.tbd.detail', {
									candShowId : cand.candShowId
								});
								$event.stopPropagation();
								$event.preventDefault();
							}
							
							$scope.interview = function(cand) {
								toInterviewService.candToInterview(cand.candId).then(
									function(result){
										if(result==true){
											var uibModal = $uibModal.open({
												animation : false,
												templateUrl : './views/candModal.html',
												size : 'lg',
												controller : [
														'$scope',
														'$uibModalInstance',
														function($scope,$uibModalInstance) {
															$scope.ok = function(event) {
																$uibModalInstance.close();
																event.preventDefault();
																event.stopPropagation();
															}
														} ]
											});
										}else{
											var uibModal = $uibModal.open({
												animation : false,
												templateUrl : './views/candInfo.html',
												size : 'lg',
												controller : [
														'$scope',
														'$uibModalInstance',
														function($scope,$uibModalInstance) {
															$scope.ok = function(event) {
																$uibModalInstance.close();
																event.preventDefault();
																event.stopPropagation();
															}
														} ]
											});
										}
								})
							}

							slideCallbackService.getPromise
									.then(
											function(result) {
												console.log('compinent resolve'
														+ result);
											},
											function(result) {
												console.log('compinent reject'
														+ result);
											},
											function(result) {
												candCandidateService()
														.then(
																function(result) {
																	$scope
																			.$watch(
																					"keyword",
																					function(
																							newValue,
																							oldValue) {
																						candCandidateService()
																								.then(
																										function(
																												result) {
																											var requestResult = filterFilter(
																													result,
																													newValue);
																											pagingFilter
																													.listLoad(
																															requestResult,
																															$scope.itemsPer);
																											$scope.totalItems = pagingFilter
																													.getPgInfo().resArylength;
																											$scope.candidates = pagingFilter
																													.getCurrentPage($scope.currPage);
																										})
																					});
																}, function() {

																});
												
											});

						} ]);
app
		.controller(
				'AddCandController',
				[
						'$scope',
						'$q',
						'$state',
						'$uibModal',
						'$window',
						'candidateService',
						'cmSlideCacheService',
						'pagingFilter',
						'filterFilter',
						'slideCallbackService',
						function($scope, $q, $state, $uibModal, $window,
								candidateService, cmSlideCacheService,
								pagingFilter, filterFilter,
								slideCallbackService) {
							$scope.cand = {
								candId : 0,
								candShowId : null,
								candName : null,
								recruiter : null,
								resource : null,
								resume : null,
								contactDate : new Date(),
								currentLocation : null,
								telphone : null,
								email : null,
								year : null,
								currentCompany : null,
								currentSalary : null,
								dep : null,
								position : null,
								status : "New",
								comments : null,
								domain : null,
								skills : null,
								language : null,
								candResume : null
							};

							$scope.addsubmit = function() {
								var cands = [];
								if ($scope.cand.email == null) {
									alert("email must have a value!!");
								} else {
									cands.push($scope.cand);
									$scope.cand.candId = 0;
									candidateService
											.candidates($scope.cand)
											.then(
													function() {
														console
																.log('add success');

														var uibModal = $uibModal
																.open({
																	animation : false,
																	templateUrl : './views/candModal.html',
																	size : 'lg',
																	controller : [
																			'$scope',
																			'$uibModalInstance',
																			function(
																					$scope,
																					$uibModalInstance) {
																				$scope.ok = function(
																						event) {
																					$uibModalInstance
																							.close();
																					event
																							.preventDefault();
																					event
																							.stopPropagation();
																				}
																			} ]
																});
														uibModal.result
																.then(
																		function() {
																			slideCallbackService.promiseDeferd
																					.notify('promise noitfy');
																			$state
																					.go('recrut2.tbd');
																		},
																		function() {
																			slideCallbackService.promiseDeferd
																					.notify('promise noitfy');
																		});
													});
								}
							}
						} ]);
app.controller('candDetailController', [
		'$scope',
		'$q',
		'$state',
		'$uibModal',
		'$stateParams',
		'getCandidateService',
		'cmSlideCacheService',
		'candUpdateService',
		'slideCallbackService',
		'dateFilter',
		function($scope, $q, $state, $uibModal, $stateParams,
				getCandidateService, cmSlideCacheService, candUpdateService,
				slideCallbackService, dateFilter) {
			console.log(slideCallbackService);

			getCandidateService($stateParams.candShowId).then(
					function(result) {
						console.log(result);
						result.contactDate = dateFilter(result.contactDate,
								'yyyy-MM-dd');
						$scope.cand = result;
					}, function() {
						console.log("success");
					});
			$scope.updateCandidate = function() {

				console.log($scope.cand);
				candUpdateService.candidate($scope.cand).then(
						function(result) {
							console.log('candUpdateService', result);
							$scope.cand = result;
							console.log(result);
							console.log("success");
							var uibModal = $uibModal.open({
								animation : false,
								templateUrl : './views/candModal.html',
								size : 'lg',
								controller : [ '$scope', '$uibModalInstance',
										function($scope, $uibModalInstance) {
											$scope.ok = function(event) {
												$uibModalInstance.close();
												event.preventDefault();
												event.stopPropagation();
											}
										} ]
							});
							uibModal.result.then(function() {
								slideCallbackService.promiseDeferd
										.notify('promise noitfy');
								$state.go('recrut2.tbd');
							}, function() {
								slideCallbackService.promiseDeferd
										.notify('promise noitfy');
							});

						}, function() {

						});
			}
		} ]);
